package com.hxd.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxd.base.BaseServiceImpl;
import com.hxd.common.enums.UserStatus;
import com.hxd.common.exception.DataException;
import com.hxd.common.utils.StringUtils;
import com.hxd.entity.SysUser;
import com.hxd.mapper.SysUserMapper;
import com.hxd.pojo.LoginVO;
import com.hxd.pojo.SysUserVO;
import com.hxd.service.SysMenuService;
import com.hxd.service.SysUserService;
import com.hxd.service.TokenService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;


/**
 * <p>
    * 用户表 服务实现类
    * </p>
 *
 * @author zhoujw
 * @since 2019-12-12
 * @version v1.0
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser> implements SysUserService {
	
	@Autowired
	private TokenService tokenService;
	
	@Autowired
	private SysMenuService sysMenuService;
	
	@Override
	public Object login(LoginVO loginVO) {
		String username = loginVO.getUsername();
		String password = loginVO.getPassword();
		if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
			throw new DataException("账号密码不能为空");
		}
		SysUser user = mapper.selectOne(new QueryWrapper<SysUser>().eq(SysUser.Fields.username, username));
		if (ObjectUtil.isNull(user)) {
			throw new DataException("用户不存在");
		}
		if (ObjectUtil.isNull(user.getPassword()) || !user.getPassword().equalsIgnoreCase(SecureUtil.md5(user.getSalt() + password))) {
			throw new DataException("密码错误");
		}
		if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
			throw new DataException("账号已被禁用");
		}
		sysMenuService.saveUserMenuCache(user.getId());
		return tokenService.createToken(user.getId());
	}
	
	@Override
	public void logout(Integer userId) {
		tokenService.removeTokenByUserId(userId);
		sysMenuService.removeMenuByUserId(userId);
	}
	
	@Override
	public void save(SysUserVO sysUserVO) {
		if (StringUtils.isEmpty(sysUserVO.getUsername())) {
			throw new DataException("账号不能为空");
		}
		if (StringUtils.isEmpty(sysUserVO.getPassword())) {
			throw new DataException("密码不能为空");
		}
		Integer count = mapper.selectCount(new QueryWrapper<SysUser>().eq(SysUser.Fields.username, sysUserVO.getUsername()));
		if(count > 0) {
			throw new DataException("用户已注册");
		}
		SysUser sysUser = new SysUser();
		BeanUtil.copyProperties(sysUserVO, sysUser);
		String salt = RandomUtil.randomString(32);
		sysUser.setPassword(SecureUtil.md5(salt + sysUser.getPassword()));
		sysUser.setSalt(salt);
		save(sysUser);
	}
	
	@Override
	public void update(SysUserVO sysUserVO) {
		SysUser user = mapper.selectById(sysUserVO.getId());
		if (ObjectUtil.isNull(user)) {
			throw new DataException("用户不存在");
		}
		BeanUtil.copyProperties(sysUserVO, user, "password");
		if(StringUtils.isNotEmpty(sysUserVO.getPassword())) {
			String salt = RandomUtil.randomString(32);
			user.setSalt(salt);
			user.setPassword(SecureUtil.md5(salt + sysUserVO.getPassword()));
		}
		mapper.updateById(user);
	}

}
